package com.gitee.sop.order.strategy.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 订单策略管理
 *
 * @author Admin
 */
@Component
public class OrderStrategyContext {
    private final Map<Integer, OrderStrategy> orderStrategyMap = new ConcurrentHashMap<>();

    @Autowired
    public OrderStrategyContext(Map<String, OrderStrategy> strategyMap) {
        this.orderStrategyMap.clear();
        strategyMap.forEach((k, v) -> this.orderStrategyMap.put(v.getKey(), v));
    }

    public OrderStrategy getOrderStrategy(Integer api) {
        Set<Integer> keySet = this.orderStrategyMap.keySet();
        for (Integer key : keySet) {
            if (api.intValue() == key.intValue()) {
                return this.orderStrategyMap.get(key);
            }
        }
        return null;
    }
}
